home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1059.dms / q1059.adf / bsplit / bsplit.c < prev    next >
C/C++ Source or Header  |  1993-08-30  |  3KB  |  127 lines

  1. /*
  2.  * bsplit.c - split binary files in manageable pieces.
  3.  * usage is exactly like the split program.
  4.  *
  5.  * This program was written from scratch, without looking at the
  6.  * sources of split.
  7.  *
  8.  * Copyright (C) 1988 P. Knoppers
  9.  *                    Bilderdijkhof 59
  10.  *                    2624 ZG  Delft
  11.  *                    The Netherlands
  12.  */
  13.  
  14. char copy0[] = "Copyright (C) 1988 P. Knoppers";
  15. char copy1[] = "Permission to use and distribute copies of this";
  16. char copy2[] = "program WITH SOURCE is granted to anyone, provided";
  17. char copy3[] = "that it is NOT CHANGED in any way.";
  18.  
  19. #include <stdio.h>
  20. #define DEFSIZE 50000
  21. #define DEFPREFIX "x"
  22. #define MAXNAME 200
  23.  
  24. char   *malloc ();
  25.  
  26. main (argc, argv)        /* bsplit - split binary file */
  27. char   *argv[];
  28. {
  29.     char   *buf;
  30.     char   *myname;
  31.     int     bulksize = DEFSIZE;
  32.     int     level;
  33.     int     got;
  34.     int     fno = 0;
  35.     char    outfname[MAXNAME + 1];
  36.     char    outbase[MAXNAME + 3];
  37.     int     foundinname = 0;
  38.     FILE * infile = stdin;
  39.     FILE * outfile;
  40.  
  41.     myname = *argv;
  42.     strcpy (outbase, DEFPREFIX);
  43.     while (--argc > 0)
  44.     {
  45.     argv++;
  46.     if ((*argv)[0] == '-')
  47.     {
  48.         if ((*argv)[1] == '\0')
  49.         {
  50.         if (foundinname != 0)
  51.         {
  52.             fprintf (stderr,
  53.                 "usage: %s [-size] [file [prefix]]\n",
  54.                 myname);
  55.             exit (1);
  56.         }
  57.         foundinname++;
  58.         }
  59.         else
  60.         if (sscanf (*argv, "-%d", &bulksize) != 1)
  61.         {
  62.             fprintf (stderr,
  63.                 "usage: %s [-size] [file [prefix]]\n",
  64.                 myname);
  65.             exit (1);
  66.         }
  67.     }
  68.     else
  69.         if (foundinname != 0)
  70.         {
  71.         if (strlen (*argv) > MAXNAME)
  72.         {
  73.             fprintf (stderr, "%s: prefix too long\n",
  74.                 myname);
  75.             exit (1);
  76.         }
  77.         strcpy (outbase, *argv);
  78.         }
  79.         else
  80.         {
  81.         if ((infile = fopen (*argv, "r")) == NULL)
  82.         {
  83.             fprintf (stderr, "%s: cannot open %s\n",
  84.                 myname, *argv);
  85.             exit (1);
  86.         }
  87.         foundinname++;
  88.         }
  89.     }
  90.  
  91.     if ((buf = malloc (bulksize)) == NULL)
  92.     {
  93.     fprintf (stderr, "%s: malloc (%u) failed: ", myname, bulksize);
  94.     perror ("");
  95.     exit (1);
  96.     }
  97.     level = 0;
  98.     while (1)
  99.     {
  100.     got = read (fileno (infile), &buf[level], bulksize - level);
  101.     level += got;
  102.     if ((level < bulksize) && (got > 0))
  103.         continue;
  104.     if ((level == bulksize) || ((got == 0) && (level > 0)))
  105.     {
  106.         sprintf (outfname, "%s%c%c", outbase, fno / 26 + 'a',
  107.             fno % 26 + 'a');
  108.         if ((outfile = fopen (outfname, "w")) == NULL)
  109.         {
  110.         fprintf (stderr, "%s: cannot create %s\n", myname,
  111.             outfname);
  112.         exit (1);
  113.         }
  114.         if (write (fileno (outfile), buf, level) != level)
  115.         {
  116.         fprintf (stderr, "%s: write failed\n", myname);
  117.         exit (1);
  118.         }
  119.         fclose (outfile);
  120.         level = 0;
  121.         fno++;
  122.     }
  123.     if (got == 0)
  124.         break;
  125.     }
  126. }
  127.